package spring.aop.xml;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * @author Yancy xiao
 * @create 2022-12-28 21:08
 */
@Component
public class LoggerAspect {

    public void beforeAdviceMethod(JoinPoint joinPoint) {
        Signature signature = joinPoint.getSignature();
        Object[] args = joinPoint.getArgs();
        System.out.println("beforeAdvice方法名：" + signature.getName() + "  参数：" + Arrays.toString(args));
    }

    public void afterAdviceMethod(JoinPoint joinPoint) {
        Signature signature = joinPoint.getSignature();
        Object[] args = joinPoint.getArgs();
        System.out.println("afterAdvice方法名：" + signature.getName() + "  参数：" + Arrays.toString(args));
    }

    public void AfterReturningAdviceMethod(JoinPoint joinPoint, Object result) {
        Signature signature = joinPoint.getSignature();
        System.out.println("AfterReturningAdvice方法名：" + signature.getName() + "   结果：" + result);
    }

    public void AfterThrowingAdviceMethod(JoinPoint joinPoint, Throwable error) {
        Signature signature = joinPoint.getSignature();
        System.out.println("AfterThrowingAdvice方法名：" + signature.getName() + "   异常通知：" + error);
    }

    public Object AroundAdviceMethod(ProceedingJoinPoint joinPoint) {
        Object proceed = null;
        try {
            System.out.println("前");
            proceed = joinPoint.proceed();
            System.out.println("返回");
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            System.out.println("异常");
        } finally {
            System.out.println("后");
        }
        return proceed;
    }
}